<template>
  <div id="box">
    <div id="search">
      <el-form :inline="true" :model="deptSearch" class="demo-form-inline">
        <el-form-item label="部门名">
          <el-input v-model="deptSearch.searchParams.deptName" placeholder="请输入部门名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSearchSubmit">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div id="header-bth">
      <el-button type="primary" size="small" icon=el-icon-circle-plus-outline @click="showAddDept">新增</el-button>
    </div>
    <div id="dept-table">
      <el-table
          :data="tableData"
          stripe
          style="width: 100%">
        <el-table-column label="部门名称">
          <template slot-scope="scope">
            <span style="margin-left: 20px">{{ scope.row.deptName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="部门地址">
          <template slot-scope="scope">
            <span>{{ scope.row.local}}</span>
          </template>
        </el-table-column>

        <el-table-column label="创建时间" width="180px">
          <template slot-scope="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280px">
          <template slot-scope="scope">
            <el-button
                icon="el-icon-edit"
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">
            </el-button>

            <el-popconfirm title="确定删除吗？" @confirm="deleteDept(scope.$index, scope.row)">
              <el-button
                  slot="reference"
                  size="mini"
                  type="danger"
                  icon="el-icon-delete">
              </el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div id="nav-page">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="pageSizes"
          :page-size="deptSearch.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!--添加 编辑部门信息弹窗-->
    <el-dialog :title="dialogTitle" :visible.sync="addDeptFormVisible" width="30%" @close="clearInfo">
      <el-form :model="deptForm" ref="deptInfo" :rules="deptRules" label-width="90px" size="medium">
        <el-input v-model="deptForm.id" autocomplete="off" v-show="false"></el-input>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="deptForm.deptName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门地址" prop="local">
          <el-input v-model="deptForm.local" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>

import {addOrUpdateDept, deleteDept, deptPage} from "@/api/dept";

export default {
  name: "DeptView",
  data() {
    return {
      tableData: [],
      addDeptFormVisible: false,
      dialogTitle: '添加部门',
      deptForm: {
        id:'',
        deptName:'',
        local:''
      },
      deptSearch: {
        limit: 3,
        page: 1,
        searchParams: {
          deptName: ''
        }
      },
      pageSizes: [3, 5, 10],
      total: 10,
      deptRules: {
        deptName: [
          {required: true, message: '请输入部门名称', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    // 改变每页显示的条数触发
    handleSizeChange(val) {
      this.deptSearch.limit = val;
      this.list()
    },
    // 改变页码触发
    handleCurrentChange(val) {
      this.deptSearch.page = val;
      this.list()
    },
    //删除部门信息
    deleteDept(index, row) {
      deleteDept(row.id).then(response=>{
        if(response.code==0){
          this.$message.success(response.message);
          this.list()
        }
      })
    },
    // 显示添加部门信息弹窗
    showAddDept() {
      this.dialogTitle = '新增'
      this.addDeptFormVisible = true
    },
    //编辑部门信息
    handleEdit(index, row) {
      this.dialogTitle = '编辑'
      this.addDeptFormVisible = true
      // 发送异步请求 获取部门信息 数据回显
      this.deptForm =row
    },
    // 添加更新部门
    saveOrUpdate() {
      this.$refs.deptInfo.validate(valid => {
        if (!valid) {
          this.$message.warning("请输入完整信息")
          return
        }
        // 发送请求完成  添加和修改操作
        addOrUpdateDept(this.deptForm).then(response=>{
          if (response.code==0){
            this.$message.success(response.message)
            this.addDeptFormVisible = false
            this.list()
          }
        })
      })
    },
    // 搜索部门信息
    onSearchSubmit() {
      this.deptSearch.currentPage = 1
      this.list()
    },
    // 关闭弹窗清空数据
    clearInfo() {
      this.deptForm = {}
      this.$refs.deptInfo.clearValidate()
    },
    // 获取部门分页数据
    list() {
      deptPage(this.deptSearch).then(response=>{
        if (response.code==0){
          let {total,tableData}   =   response.data
          this.total = total
          this.tableData = tableData
        }
      })
    }
  },
  created() {
    this.list()
  }
}
</script>

<style scoped>
#box {
  width: 95%;
  margin: 5px auto 0;
}

#search {
  margin-top: 10px;
  padding: 5px;
  border-bottom: 1px solid #eee;
}

#nav-page {
  margin-top: 15px;
  margin-bottom: 30px;
}

#header-bth {
  margin-top: 20px;
  margin-bottom: 20px;
}

.el-button {
  margin-left: 5px;
  margin-right: 5px;
}
</style>